// pages/home/home.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    activeColor:"#DD4E40",
    vertical: false,
    autoplay: false,
    duration: 500,
    circular: false,
    app: getApp(),
    searchKey:[],
    typeList: [],//二级分类
    typeListMul: 0//二级分类8的倍数
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    getApp().initToken(function(){
      that.querySearchKeys();
      that.getHomeTypeList();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
   * 扫一扫
   */
  scan: function () {
    wx.scanCode({
      success: (res) => {
        //this.searchBusiness(res.result)
        wx.navigateTo({
          url: '../businessList/businessList?searchKey=' + res.result
        })
      }
    })
  },

  /**
   * 跳转搜索页面
   */
  navigateToSearchPage: function(){
    wx.login({
      success: function (res) {
        console.log(res.code)

        //发起网络请求
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session',
          data: {
            'appid': 'wx1c35321f771c0c31',
            'secret': '87a61920c73a6a73ed11cd65d8c74469',
            'js_code': res.code,
            'grant_type': 'authorization_code'
          },
          method: 'GET',
          success: function (res) {
            console.log(JSON.stringify(res.data));
          }
        })
      }
    })

    return;
    wx.navigateTo({
      url: '../search/search',
    })
  },

  /**
   * 搜索关键词
   */
  querySearchKeys: function(){
    var that = this;
    wx.request({
      url: that.data.app.data.server + that.data.app.data.url.getSearchKeyList,
      header: {
        'Token': getApp().getToken()//that.data.app.getToken()
      },
      success: function(res) {
        that.setData({
          searchKey: res.data.Data
        })
      }
    })
  },

  /**
   * 查询-首页分类及二级分类
   */
  getHomeTypeList: function() {
    let that = this;
    wx.request({
      url: that.data.app.data.server + that.data.app.data.url.getHomeTypeList,
      header: {
        'Token': getApp().getToken()
      },
      success: function(res) {
        if (res.data.Successed==true){
          let data = res.data.Data;
          let num = Math.ceil(res.data.Data.length / 10);
          //封装数据
          let tempData = new Array();
          for(let i=0; i<num; i++) {
            let tempArray = new Array();
            for(let j=0; j<10; j++) {
              if (data.length > (i * 10 + j)) {
                tempArray.push(data[i * 10 + j]);
              }else{
                break;
              }
            }
            tempData.push(tempArray);
          }
          that.setData({
            typeList: tempData
          })
        }
      }
    })
  }

})